﻿using Dapper;
using Management.Systeam.Domaina;
using Management.Systeam.ErrorCode;
using Management.Systeam.Infrastructure.Implement;
using Management.Systeam.Infrastructure.Interface;
using Management.Systeam.Read.Apis.Application.Command.InventoryAudits;
using Management.Systeam.Read.Apis.Application.Command.Useres;
using Management.Systeam.Read.Apis.Auto.UserDto;
using MediatR;
using System.Text;

namespace Management.Systeam.Read.Apis.Application.CommandHandler.InventoryAudits
{
    public class ShowInventoryAuditsCommandHandler : IRequestHandler<ShowInventoryAuditsCommand, ApiPeaging<ApiResult<List<InventoryAudit>>>>
    {
        IBaseRepository<InventoryAudit> InventoryAudit;
        public DatabaseContext db;

        public ShowInventoryAuditsCommandHandler(IBaseRepository<InventoryAudit> InventoryAudit, DatabaseContext db)
        {
            this.InventoryAudit = InventoryAudit;
            this.db = db;
        }

        public async Task<ApiPeaging<ApiResult<List<InventoryAudit>>>> Handle(ShowInventoryAuditsCommand request, CancellationToken cancellationToken)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("SELECT * FROM InventoryAudit");
            sb.Append(" WHERE IsDel LIKE '%" + 0 + "%'");

            if (request.InventoryAudit_Code != null)
            {
                sb.Append(" AND InventoryAudit_Code LIKE '%" + request.InventoryAudit_Code + "%'");
            }
            if (request.Inventory_Result != null)
            {
                sb.Append(" AND Inventory_Result LIKE '%" + request.Inventory_Result + "%'");
            }
            if (request.Inventory_Type != null)
            {
                sb.Append(" AND Inventory_Type LIKE '%" + request.Inventory_Type + "%'");
            }
            if (request.Warehouse_Name != null)
            {
                sb.Append(" AND Warehouse_Name LIKE '%" + request.Warehouse_Name + "%'");
            }
            var paramet = new
            {
                InventoryAudit_Code = request.InventoryAudit_Code,
                Inventory_Result = request.Inventory_Result,
                Inventory_Type = request.Inventory_Type,
                Warehouse_Name = request.Warehouse_Name,
                PageSize = request.PageSize,
                offset = (request.PageIndex - 1) * request.PageSize
            };
            var qtlist = await db.DapperConnection.QueryAsync<InventoryAudit>(sb.ToString(), paramet);
            int total = qtlist.Count();
            sb.Append(" ORDER BY InventoryAudit_Id DESC LIMIT @PageSize OFFSET @offset");
            var pageData = await db.DapperConnection.QueryAsync<InventoryAudit>(sb.ToString(), paramet);
            int totalPage = (int)Math.Ceiling(total / (double)request.PageSize);

            return new ApiPeaging<ApiResult<List<InventoryAudit>>>
            {
                Total = total,
                TotalPage = totalPage,
                Data = new ApiResult<List<InventoryAudit>>
                {
                    Code = ApiEnum.success,
                    Message = "查询成功",
                    Data = pageData.ToList()
                }
            };
        }
    }
}
